import 'package:flutter/material.dart';

class UserTabVc extends StatefulWidget {
  @override
  _UserTabVcState createState() => _UserTabVcState();
}

class _UserTabVcState extends State<UserTabVc>
    with SingleTickerProviderStateMixin {
  // AnimationController _controller;
  TabController _controller;
  int _selectedIndex = 0;
  Offset starOffset = Offset(0, 0);
  Offset endOffset = Offset(0, 0);

  bool showBG = true;

  @override
  void initState() {
    super.initState();
    // _controller = AnimationController(vsync: this);
    _controller = TabController(length: 3, vsync: this);
    _controller.addListener(() => _onTabChanged());
  }

  void _onTabChanged() {
    Future.delayed(Duration(milliseconds: 100), () {
      if (_controller.index.toDouble() == _controller.animation.value) {
        _selectedIndex = _controller.index;
        print(_selectedIndex);
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return item();
  }

  Widget item() {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        backgroundColor: Colors.grey[100],
        appBar: AppBar(
          title: Text('Muzico'),
          elevation: 0.0,
          leading: IconButton(
            icon: Icon(Icons.menu),
            tooltip: 'Navigation',
            onPressed: () => debugPrint('Navigation button is pressed.'),
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              tooltip: 'Search',
              onPressed: () => debugPrint('Search button is pressed.'),
            ),
          ],
          bottom: TabBar(
            // controller: _controller,
            unselectedLabelColor: Colors.black,
            indicatorColor: Colors.white,
            indicatorSize: TabBarIndicatorSize.label,
            indicatorWeight: 0.5,
            tabs: <Widget>[
              Tab(
                // text: '全部项目',
                // icon: Icon(Icons.local_bar),
                child: Text('全部项目'),
              ),
              Tab(
                // text: '由我负责',
                // icon: Icon(Icons.local_cafe),
                child: Text('由我负责'),
              ),
              Tab(
                // text: '向我汇报',
                // icon: Icon(Icons.local_offer),
                child: Text('向我汇报'),
              ),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            Icon(
              Icons.local_bar,
              size: 128.0,
              color: Colors.black12,
            ),
            Icon(
              Icons.local_cafe,
              size: 128.0,
              color: Colors.black12,
            ),
            Icon(
              Icons.local_offer,
              size: 128.0,
              color: Colors.black12,
            ),
          ],
        ),
      ),
    );
  }

  Widget item1() {
    return Listener(
      onPointerMove: (event) {
        if ((event.position.dx - starOffset.dx).abs() >
            (event.position.dy - starOffset.dy).abs()) {
          // print('横向');
          // print(event.position.dx - starOffset.dx);
          // print(event.position.dy - starOffset.dy);
        }
      },
      onPointerDown: (event) {
        print('onPointerDown: ${event.position}');
        starOffset = event.position;
        showBG = false;
        setState(() {});
      },
      onPointerUp: (event) {
        endOffset = event.position;
        Future.delayed(Duration(milliseconds: 200), () {
          showBG = true;
          setState(() {});
        });

        print('onPointerUp: ${event.position}');
        if ((event.position.dx - starOffset.dx) >
            (event.position.dy - starOffset.dy)) {
          print('11111');
        } else {
          print('22222');
        }
      },
      child: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.red,
            title: Text('data'),
            bottom: TabBar(
              tabs: <Widget>[
                Tab(
                  child: Container(
                    color: (0 == _selectedIndex && showBG)
                        ? Colors.orange
                        : Colors.transparent,
                    child: Text(
                      "全部项目",
                      style: TextStyle(
                          color: 0 == _selectedIndex
                              ? Colors.black
                              : Colors.white),
                    ),
                  ),
                ),
                Tab(
                  child: Container(
                    color: (1 == _selectedIndex && showBG)
                        ? Colors.orange
                        : Colors.transparent,
                    child: Text(
                      "由我负责",
                      style: TextStyle(
                          color: 1 == _selectedIndex
                              ? Colors.black
                              : Colors.white),
                    ),
                  ),
                ),
                Tab(
                  child: Container(
                    color: (2 == _selectedIndex && showBG)
                        ? Colors.orange
                        : Colors.transparent,
                    child: Text(
                      "向我汇报",
                      style: TextStyle(
                          color: 2 == _selectedIndex
                              ? Colors.black
                              : Colors.white),
                    ),
                  ),
                )
              ],
            ),
          ),
          body: TabBarView(
            controller: _controller,
            children: <Widget>[
              Center(child: Text("全部项目")),
              Center(child: Text("由我负责")),
              Center(child: Text("向我汇报")),
            ],
          ),
        ),
      ),
    );
  }
}
